package com.example.zg3_park.news

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_base.entity.NewsEntityItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.zg3_park.R
import com.example.zg3_park.databinding.ActivityNewsBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/app/NewsActivity")
@AndroidEntryPoint
class NewsActivity : BaseActivity<ActivityNewsBinding,NewsViewModel>() {
    val newsAdapter by lazy { NewsAdapter() }

    override fun intiView() {
        binding.rvNews.apply {
            adapter = newsAdapter
            layoutManager = LinearLayoutManager(this@NewsActivity)
        }

        //删除
        newsAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, item: NewsEntityItem) {
                //binding.rvNews.clearOnScrollListeners()
                binding.rvNews.clearAnimation()
                newsAdapter.notifyItemRemoved(position)
            }

        })
    }

    override fun intiData() {
        lifecycleScope.launch {
            vm.getNews().collect() {
                newsAdapter.submitData(it)
            }
        }

    }
}